﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using yncTech.Common;
using yncTech.BusinessObjects;

public partial class Admin_SAPFin_SAPFin002a : AdminBasePage
{
    decimal[] boxID;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString[VBoxReceiptsMetadata.ColumnNames.BoxID] != null)
        {
            boxID = Request.QueryString[VBoxReceiptsMetadata.ColumnNames.BoxID].TrimEnd(',').Split(',').Select(r=>decimal.Parse(r)).ToArray();
        }
        if (!IsPostBack)
        {
            drpYearStart.SelectedValue = DateTime.Now.Year.ToString();

            switch (ActionType)
            {
                case OperationType.Add:
                    //GetBoxNo();
                    btnSave.Text = "新增";
                    break;
                case OperationType.Update:
                    if (boxID.Length > 0) {
                        if (boxID.Length == 1)
                        {
                            btnSave.Text = "修改";
                            VBoxReceipts vbox = new VBoxReceipts();
                            vbox.Query.Where(vbox.Query.BoxID == boxID);
                            vbox.Query.Load();
                            drpYearStart.SelectedValue = vbox.BoxYear.ToString();
                            drpMonthStart.SelectedValue = vbox.BoxMonth.ToString();
                            drpDocType.SelectedValue = vbox.DocumentType;
                            drpLocation.SelectedValue = vbox.BoxLocation.ToString();
                            drpFunctionDept.SelectedValue = vbox.FunctionDept;
                            drpCompany.SelectedValue = vbox.CompanyCode;
                            if (vbox.ReceiptCount.HasValue && vbox.ReceiptCount.Value > 0)
                            {
                                drpDocType.Enabled = false;
                                drpFunctionDept.Enabled = false;
                                drpLocation.Enabled = false;
                            }
                            drpCompany.Enabled = false;
                        }
                        else {
                            btnSave.Text = "批量修改";
                        }
                    }                  

                    //if (vbox.IsOpen.HasValue && vbox.IsOpen.Value)
                    //{
                    //    radYes.Checked = true;
                    //}
                    //else
                    //{
                    //    radNo.Checked = true;
                    //}

                    break;
                case OperationType.Delete:
                    btnSave_Click(null, null);
                    break;

            }

        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            lbMessage.Text = "";
            FinBox box = new FinBox();
            VBoxReceiptsCollection boxreceiptscollection = new VBoxReceiptsCollection();
            boxreceiptscollection.Query.Where(boxreceiptscollection.Query.BoxID.In(boxID));
            boxreceiptscollection.Query.Load();
            switch (ActionType)
            {
                case OperationType.Add:
                    //int index = 1;
                    //FinBox lastBox = new FinBox();
                    //lastBox.Query.Where(lastBox.Query.CompanyCode == drpCompany.SelectedValue).OrderBy(lastBox.Query.BoxID.Descending);
                    //lastBox.Query.es.Top = 1;
                    //lastBox.Query.Load();
                    //if (lastBox.es.HasData)
                    //{
                    //    if (lastBox.CreatedDttm.Value.Year == DateTime.Now.Year)
                    //    {
                    //        index = lastBox.BoxIndex.Value + 1;
                    //    }
                    //}
                    box.CreatedBy = box.UpdatedBy = LoginUserInfo.CurrentUser.UserName;
                    box.UpdatedDttm = box.CreatedDttm = DateTime.Now;
                    box.Save();
                    break;
                case OperationType.Update:
                    if (boxID.Length == 1)
                    {
                        box.LoadByPrimaryKey(boxID[0]);
                        //if (drpLocation.Enabled)
                        //{
                        //    if (!string.IsNullOrEmpty(drpLocation.SelectedValue))
                        //    {
                        //        box.BoxLocation = decimal.Parse(drpLocation.SelectedValue);
                        //    }
                        //    box.FunctionDept = drpFunctionDept.SelectedValue;
                        //    box.DocumentType = drpDocType.SelectedValue;
                        //}
                        box.UpdatedBy = LoginUserInfo.CurrentUser.UserName;
                        box.UpdatedDttm = DateTime.Now;

                        break;
                    }
                    else {
                        foreach (var boxreceipt in boxreceiptscollection) {
                            box.UpdatedBy = LoginUserInfo.CurrentUser.UserName;
                            box.UpdatedDttm = DateTime.Now;
                            break;
                        }
                    }
                    break;

                   
                case OperationType.Delete:
                    if (boxID.Length == 1)
                    {
                        box.LoadByPrimaryKey(boxID[0]);
                        box.IsDeleted = true;

                        //WebHelper.GotoParent();
                    }    
                        break;             

            }
            if (!string.IsNullOrEmpty(drpCompany.SelectedValue))
            {
                box.CompanyName = drpCompany.SelectedValue.Split('/')[0];
                box.CompanyCode = drpCompany.SelectedValue.Split('/')[1];
            }
            
            box.BoxYear = box.BoxYearEnd = int.Parse(drpYearStart.SelectedValue);
            box.BoxMonth = int.Parse(drpMonthStart.SelectedValue);
            if (!string.IsNullOrEmpty(drpMonthEnd.SelectedValue))
            { box.BoxMonthEnd = int.Parse(drpMonthEnd.SelectedValue); }
            box.StorageStatus = "S0";//新建的凭证盒
            if (!string.IsNullOrEmpty(drpLocation.SelectedValue))
            {
                box.BoxLocation = decimal.Parse(drpLocation.SelectedValue);
            }
            if (!string.IsNullOrEmpty(drpCabin.SelectedValue))
            {
                box.ThirdUnitCabinId = Guid.Parse(drpCabin.SelectedValue);
            }
            box.FunctionDept = drpFunctionDept.SelectedValue;
            box.DocumentType = drpDocType.SelectedValue;
            box.IsSap = true;
            box.IsHis = false;
            box.Save();

            WebHelper.CloseDialog(true);
        }
        catch (Exception ex)
        {
            lbMessage.Text = ex.Message;
        }
    }


    protected void drpLocation_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(drpLocation.SelectedValue))
        {
            BFThirdUnitCabinCollection bFThirdUnitCabinCollection = new BFThirdUnitCabinCollection();
            bFThirdUnitCabinCollection.Query.Where(bFThirdUnitCabinCollection.Query.ThirdUnitID == drpLocation.SelectedValue)
                .OrderBy(bFThirdUnitCabinCollection.Query.Name.Ascending);
            bFThirdUnitCabinCollection.Query.Load();
            drpCabin.DataSource = bFThirdUnitCabinCollection;
            drpCabin.DataTextField = BFThirdUnitCabinMetadata.ColumnNames.Name;
            drpCabin.DataValueField = BFThirdUnitCabinMetadata.ColumnNames.ThirdUnitCabinID;
            drpCabin.DataBind();

            drpCabin.Items.Insert(0, new ListItem("请选择凭证仓库号", ""));
        }
    }
}